package com.lattice.result;

import com.alibaba.fastjson.JSONObject;
import com.lattice.enums.FeignErrorEnum;
import com.lattice.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Collections;

/**
 * @author mis_wu
 * @date 2022/6/18 15:37
 * @todo 统一返回封装实体
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("all")
public class ResponseResult<T> implements Serializable{

    static final long serialVersionUID = -7430439691155161871L;

    private static Logger log = LoggerFactory.getLogger(ResponseResult.class);

    private static JSONObject OBJECT_EMPTY = new JSONObject();


    private String code;
    private String msg;
    private T data;

    public static <T> ResponseResult<T> isSuccess(){
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultEnum.RESULT_SUCCESS.getCode());
        result.setMsg(ResultEnum.RESULT_SUCCESS.getMsg());
        result.setData((T) Collections.EMPTY_LIST);
        return result;
    }

    public static <T> ResponseResult<T> isSuccess(T data){
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultEnum.RESULT_SUCCESS.getCode());
        result.setMsg(ResultEnum.RESULT_SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> ResponseResult<T> isSuccess(String code, String msg, T data){
        ResponseResult<T> result = new ResponseResult<>();
        if (ObjectUtils.isEmpty(code)){
            code = ResultEnum.RESULT_SUCCESS.getCode();
        }
        if (ObjectUtils.isEmpty(msg)){
            msg = ResultEnum.RESULT_SUCCESS.getMsg();
        }
        if (ObjectUtils.isEmpty(data)){
            data = (T) Collections.singletonList(data);
        }
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static<T> ResponseResult<T> error(){
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultEnum.RESULT_ERROR.getCode());
        result.setMsg(ResultEnum.RESULT_ERROR.getMsg());
        result.setData((T) Collections.singletonList(Collections.EMPTY_LIST));
        return result;
    }

    public static<T> ResponseResult<T> error(T error, String ... msg){
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultEnum.RESULT_ERROR.getCode());
        if (msg != null){
            String val = "";
            for (String m : msg) {
                val = val.concat(m.concat(","));
            }
            if (val.trim().endsWith(",")){
                //截取调最后一个逗号
                val = val.trim().substring(0,val.trim().lastIndexOf(",") );
            }
            result.setMsg(val);
        }else {
            result.setMsg(ResultEnum.RESULT_ERROR.getMsg());
        }
        result.setData(error);
        return result;
    }

    public static<T> ResponseResult<T> error(String code,String error,T data){
        ResponseResult<T> result = new ResponseResult<>();
        if (ObjectUtils.isEmpty(code)){
            code = ResultEnum.RESULT_ERROR.getCode();
        }
        if (ObjectUtils.isEmpty(error)){
            error = ResultEnum.RESULT_ERROR.getMsg();
        }
        if (ObjectUtils.isEmpty(data)){
            data = (T) Collections.EMPTY_LIST;
        }
        result.setCode(code);
        result.setMsg(error);
        result.setData(data);
        return result;
    }

    public static<T> ResponseResult<T> feignError(FeignErrorEnum feignErrorEnum, Object throwable){
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(feignErrorEnum.getCode());
        result.setMsg(feignErrorEnum.getDesc());
        if (throwable instanceof Throwable){
            result.setData((T) ((Throwable) throwable).getMessage());
        }else {
            result.setData((T) throwable);
        }
        return result;
    }

}
